<?php
/** 
*
* @package phpBB3
* @version $Id: functions_topic_tags.php 89 2007-07-04 16:48:38Z highwayoflife $
* @copyright (c) 2007 Star Trek Guide Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
 * Processing function for mcp_main
 * Sets the topic tag if an action is specified and permissions are met -- acts on quickmod actions
 *
 * @return false if there is no action
 */
function process_mcp_topic_tags_main()
{
	global $user, $db, $user, $action;
	global $config, $forum_id, $quickmod, $phpEx;
	
	if (!$config['topic_tags_version'])
	{
		return false;
	}
	if (!$action)
	{
		return false;
	}
	
	$topic_tag_id = (int) substr(strrchr($action, '_'), 1);
	
	if (!$topic_tag_id || strpos($action, 'topic_tag') !== 0)
	{
		return false;
	}
	
	$ids = (!$quickmod) ? request_var('topic_id_list', array(0)) : array(request_var('t', 0));

	if (!sizeof($ids))
	{
		trigger_error('NO_TOPIC_SELECTED');
	}
	
	$tag_data = topic_tags_list($forum_id, true, $user->data['user_id'], true);
	
	$redirect = request_var('redirect', build_url(array('_f_', 'action', 'quickmod')));
	
	$s_hidden_fields = build_hidden_fields(array(
		'topic_id_list'		=> $ids,
		'action'			=> $action,
		'redirect'			=> $redirect)
	);
	$success_msg = '';
	
	
	$user->add_lang('mods/permissions_topic_tags');
	
	if (confirm_box(true))
	{
		$sql = 'UPDATE ' . TOPICS_TABLE . ' SET topic_tag_id = ' . $topic_tag_id . '
				WHERE ' . $db->sql_in_set('topic_id', $ids);
		$db->sql_query($sql);

		$data = get_topic_data($ids);

		foreach ($data as $row)
		{
			add_log('mod', $row['forum_id'], $row['topic_id'], 'LOG_SET_TOPIC_TAG', strip_tags($tag_data['topic_tag_' . $topic_tag_id]), $row['topic_title']);
		}

		$success_msg = $user->lang[((sizeof($ids) == 1) ? 'TOPIC' : 'TOPICS') . '_TAG_SET_SUCCESS'];
	}
	else
	{
		confirm_box(false, $user->lang['CONFIRM_SET_TOPIC' . ((sizeof($ids) == 1) ? '' : 'S') . '_TAG'], $s_hidden_fields);
	}

	$redirect = request_var('redirect', 'index.' . $phpEx);
	$redirect = reapply_sid($redirect);

	if (!$success_msg)
	{
		redirect($redirect);
	}
	else
	{
		meta_refresh(2, $redirect);
		trigger_error($success_msg . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
	}
}

/**
 * Process functions specifically for the quickmod actions for topic tags
 *
 * @param int $forum_id
 * @param int $topic_id
 */
function process_mcp_topic_tags($forum_id, $topic_id)
{
	global $user, $auth, $module;
	global $config, $quickmod, $action;
	
	if (!$config['topic_tags_version'])
	{
		return false;
	}
	
	// if the user cannot read the forum he tries to access then we won't allow mcp access either
	// this is processed later, but due to the fact that this will redirect prior to reaching that point, we do this now...
	if ($forum_id && !$auth->acl_get('f_read', $forum_id))
	{
		trigger_error($user->lang['NOT_AUTHORISED']);
	}
	
	$tag_data = topic_tags_list($forum_id, true, $user->data['user_id'], true);
	
	if (!$tag_data)
	{
		return false;
	}
	
	$user_quickmod_actions = array_combine(array_keys($tag_data), array_fill(0, sizeof($tag_data), 'f_edit_topic_tag'));
	
	$allow_user = false;
	
	if ($auth->acl_get('m_edit_topic_tags', $forum_id))
	{
		$allow_user = true;
	}
	else if ($quickmod && isset($user_quickmod_actions[$action]) && $user->data['is_registered'] && $auth->acl_gets($user_quickmod_actions[$action], $forum_id))
	{
		$topic_info = get_topic_data(array($topic_id));
		if ($topic_info[$topic_id]['topic_poster'] == $user->data['user_id'])
		{
			$allow_user = true;
		}
	}
	
	if (!$allow_user)
	{
		trigger_error('NOT_AUTHORISED');
	}
	else 
	{
		if ($quickmod)
		{
			$mode = 'quickmod';
			
			if (in_array($action, array_keys($tag_data)))
			{
				$module->load('mcp', 'main', 'quickmod');
				exit;
			}
		}
	}
}

/**
 * Generate a list of available topic tags for the current forum
 * Customize list depending on if the current view is the MCP -- set current tag if posting
 *
 * @param int $forum_id				forum currently browsing
 * @param bool $forum_topic_tags	if topic tags are allowed in this forum
 * @param int $topic_poster			topic author id
 * @param bool $in_mcp				if this list is in the MCP and/or in quickmod tools
 * @param bool $in_posting			if this list is in posting.php
 * @param int $current_tag			Current tag (if any) -- used for editing posts
 * @return false, options list, or array
 */
function topic_tags_list($forum_id, $forum_topic_tags, $topic_poster = false, $in_mcp = false, $in_posting = false, $current_tag = false)
{
	global $db, $auth, $user, $config;
	
	if (!$forum_topic_tags || !$config['topic_tags_version'])
	{
		return false;
	}
	
	$show_tags = ($auth->acl_get('f_set_topic_tag', $forum_id) && $in_posting) ? true : false;
	$show_tags = ($auth->acl_get('m_edit_topic_tags', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_poster && $auth->acl_get('f_edit_topic_tag', $forum_id))) ? true : $show_tags;

	if (!$show_tags)
	{
		return false;
	}
	
	$user->add_lang('mods/permissions_topic_tags');
	
	$mod_list = ($in_mcp) ? array() : (($in_posting) ? '<option value="topic_tag_0">' . $user->lang['NO_TAG'] . "</option>\n" : '');
	
	$sql = 'SELECT *
			FROM ' . TOPIC_TAGS_TABLE . '
			WHERE topic_tag_global = 1 ' . (($forum_id) ? ' OR forum_id = ' . $forum_id : '');
	$result = $db->sql_query($sql, 3600);
	
	while ($row = $db->sql_fetchrow($result))
	{
		if (!$in_mcp)
		{
			$selected = ((int) $current_tag == $row['topic_tag_id']) ? ' selected="selected"' : '';
			$mod_list .= '<option value="topic_tag_' . $row['topic_tag_id'] . '"' . $selected . '>' . sprintf($user->lang['TAG_AS'], strip_tags(html_entity_decode($row['topic_tag_name']))) . '</option>' . "\n";
		}
		else 
		{
			$mod_list['topic_tag_' . $row['topic_tag_id']] = $row['topic_tag_name'];
		}
	}
	
	return $mod_list;
}

function topic_tags_title(&$topic_data, $forum_topic_tags = false, $topic_title = false)
{
	global $config;
	
	if ($config['topic_tags_version'])
	{
		if (!$forum_topic_tags)
		{
			return false;
		}
		else 
		{
			$topic_tag = (!empty($topic_data['topic_tag_colour'])) ? '<span style="color:#' . $topic_data['topic_tag_colour'] . ';">' . html_entity_decode($topic_data['topic_tag_name']) . '</span>' : html_entity_decode($topic_data['topic_tag_name']);
			
			if ($topic_title)
			{
				global $template;
				
				$template->assign_vars(array(
					$topic_title => $topic_tag . ' ' . $template->_rootref['TOPIC_TITLE'],
					)
				);
			}
			else 
			{
				$topic_data['topic_title'] = $topic_tag . ' ' . censor_text($topic_data['topic_title']);
			}
		}
	}
}

function posting_topic_tags_list($post_data, $topic_id = 0)
{
	global $config, $auth, $mode, $template;
	global $user, $forum_id, $db;
	
	if (!$config['topic_tags_version'] || !$post_data['forum_topic_tags'] || !in_array($mode, array('reply', 'quote', 'edit', 'post')))
	{
		return false;
	}
	
	$show_topic_tags = false;
	
	if ($auth->acl_get('m_edit_topic_tags', $forum_id))
	{
		$show_topic_tags = true;
	}
	else if ($auth->acl_get('f_set_topic_tag', $forum_id) && $mode == 'post')
	{
		$show_topic_tags = true;
	}
	else if ($auth->acl_get('f_edit_topic_tag', $forum_id) && $post_data['topic_poster'] == $user->data['user_id'])
	{
		$show_topic_tags = true;
	}
	$current_tag = ($mode == 'post') ? 0 : $post_data['topic_tag_id'];
	
	$template->assign_vars(array(
		'S_TOPIC_TAGS'			=> $show_topic_tags,
		'S_TOPIC_TAGS_OPTIONS'	=> topic_tags_list($forum_id, $post_data['forum_topic_tags'], false, false, true, $current_tag),
	));
	
	$topic_tag_id = request_var('topic_tag', '');
	
	if ($show_topic_tags && $topic_id && $topic_tag_id)
	{
		$topic_tag_id = substr(strrchr($topic_tag_id, '_'), 1);
			
		$sql = 'UPDATE ' . TOPICS_TABLE . ' SET topic_tag_id = ' . (int) $topic_tag_id . ' WHERE topic_id = ' . $topic_id;
		$db->sql_query($sql);
	}
}

/**
 * for posting.php -- sets topic tag based on proper permissions
 *
 * @param array $sql_data
 * @param int $forum_id
 * @return $sql_data
 */
function insert_topic_tag(&$sql_data, $data)
{
	global $config, $auth, $mode, $user, $db;
	
	if ($config['topic_tags_version'])
	{
		$topic_poster = 0;
		if (in_array($mode, array('reply', 'edit')))
		{
			$sql = 'SELECT topic_poster
					FROM ' . TOPICS_TABLE . '
					WHERE topic_id = ' . $data['topic_id'];
			$result = $db->sql_query($sql);
			$topic_poster = $db->sql_fetchfield('topic_poster');
			$db->sql_freeresult($result);
		}
		
		$forum_id = $data['forum_id'];
		$set_topic_tag = false;

		if ($auth->acl_get('m_edit_topic_tags', $forum_id))
		{
			$set_topic_tag = true;
		}
		else if ($auth->acl_get('f_edit_topic_tag', $forum_id) && in_array($mode, array('edit', 'reply')) && $user->data['user_id'] == $topic_poster)
		{
			$set_topic_tag = true;
		}
		else if ($auth->acl_get('f_set_topic_tag', $forum_id) && $mode == 'post')
		{
			$set_topic_tag = true;
		}
		
		$topic_tag = request_var('topic_tag', '');
		$topic_tag = substr(strrchr($topic_tag, '_'), 1);
		
		if ($set_topic_tag && $topic_tag)
		{
			$sql_data[TOPICS_TABLE]['sql']['topic_tag_id'] = $topic_tag;
		}
	}
}

function manage_forum_topic_tags($forum_data)
{
	global $config, $template;
	
	if (!$config['topic_tags_version'])
	{
		return false;
	}
	
	$template->assign_vars(array(
		'S_FORUM_TOPIC_TAGS_MOD'	=> true,
		'S_FORUM_TOPIC_TAGS'		=> (!isset($forum_data['forum_topic_tags'])) ? false : (($forum_data['forum_topic_tags']) ? true : false),
	));
}

/**
 * Function to left join the topic tags to the topic
 *
 * @param array $sql_array	ref: SQL query array
 */
function get_forum_topic_tags(&$sql_array)
{
	global $config;
	
	if ($config['topic_tags_version'])
	{
		$sql_array['SELECT'] .= ', tg.topic_tag_name, tg.topic_tag_colour';
		$sql_array['LEFT_JOIN'][] = array(
			'FROM'	=> array(TOPIC_TAGS_TABLE => 'tg'),
			'ON'	=> 'tg.topic_tag_id = t.topic_tag_id'
		);
	}
}

function viewtopic_topic_tags($forum_id, $forum_topic_tags, $topic_poster, $topic_data, $tpl_var)
{
	global $config, $template, $auth, $user;
	
	if (!$config['topic_tags_version'] || !$forum_topic_tags)
	{
		return false;
	}
	
	if ($auth->acl_get('m_edit_topic_tags', $forum_id) || ($auth->acl_get('f_edit_topic_tag', $forum_id) && $topic_poster == $user->data['user_id']))
	{
		$topic_tag_list = topic_tags_list($forum_id, $forum_topic_tags, $topic_poster);
		$template->assign_vars(array(
			'S_TOPIC_MOD' => (!empty($template->_rootref['S_TOPIC_MOD'])) ? str_replace('</select>', $topic_tag_list, $template->_rootref['S_TOPIC_MOD']) : '<select name="action">' . $topic_tag_list . '</select>',
		));
	}
	topic_tags_title($topic_data, $forum_topic_tags, $tpl_var);
}
?>